package edu.fit5046.mymonashmate.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class HttpUtils {

	private static final String TAG = "HttpUtils";
	public static final String IP = "118.139.26.155";
	public static final String BASE_URI = "http://" + IP
			+ ":8080/MonashMateServer/webresources";

	public static String Get(String url) {

		String URL = BASE_URI + url;

		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(URL);

		try {
			Log.i(TAG, URL);
			HttpResponse response = client.execute(request);
			InputStream input = response.getEntity().getContent();
			String result = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));
			StringBuilder sb = new StringBuilder();
			while ((result = reader.readLine()) != null) {
				sb.append(result);
			}
			Log.i(TAG, sb.toString());
			Log.i(TAG, Integer.toString(response.getStatusLine().getStatusCode()));
			System.out.println(response.getStatusLine());

			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String Post(String url, String json) {

		String URL = BASE_URI + url;

		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(URL);
		request.setHeader("Content-type", "application/json");
		request.setHeader("Accept", "application/json");
		try {
			Log.i(TAG, URL);
			StringEntity entity_data = new StringEntity(json);
			request.setEntity(entity_data);
			HttpResponse response = client.execute(request);
			Log.i(TAG, Integer.toString(response.getStatusLine().getStatusCode()));
			if (response.getStatusLine().getStatusCode() == 204||response.getStatusLine().getStatusCode() == 200) {
				return "success";
			} else {
				return "failure";
			}

		} catch (Exception e) {
			e.printStackTrace();
			return "failure";
		}

	}

	public static String Edit(String url, String json) {

		String URL = BASE_URI + url;

		HttpClient client = new DefaultHttpClient();
		HttpPut request = new HttpPut(URL);
		request.setHeader("Content-type", "application/json");
		request.setHeader("Accept", "application/json");

		try {
			Log.i(TAG, URL);
			StringEntity entity_data = new StringEntity(json);
			request.setEntity(entity_data);
			HttpResponse response = client.execute(request);
			Log.i(TAG, Integer.toString(response.getStatusLine().getStatusCode()));

			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "failure";
		}
	}

	public static String Delete(String url, String json) {

		String URL = BASE_URI + url + json;

		HttpClient client = new DefaultHttpClient();
		HttpDelete request = new HttpDelete(URL);

		try {
			Log.i(TAG, URL);
			HttpResponse response = client.execute(request);
			Log.i(TAG, Integer.toString(response.getStatusLine().getStatusCode()));
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "failure";
		}
	}
}
